import { defineStore } from 'pinia'
import { storage } from '@/utils/storage'
import { nextTick } from 'vue'

export const useAppStore = defineStore({
  id: 'app',
  state: () => ({
    isCollapse: false,
    isMobile: false,
    reloadFlag: true,
    language: storage.get('lang') ?? 'zh-CN'
  }),
  actions: {
    setIsCollapse(collapse: boolean) {
      this.isCollapse = collapse
    },
    setIsMobile(mobile: boolean) {
      this.isMobile = mobile
    },
    setReloadFlag() {
      this.reloadFlag = false
      // dom 重新渲染后
      nextTick(() => {
        this.reloadFlag = true
      })
    },
    setLanguage(lang: 'zh-CN' | 'en') {
      this.language = lang
      storage.set('lang', lang)
    }
  }
})
